/*
 * 文 件    名:  MainActivityServiceProviderImpl.java
 * 创建日期:  2013-3-18下午9:24:07
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2013,  All rights reserved
 * 作          者:  xrj66010
 */
package com.weipu.common.facade.impl;

import java.util.ArrayList;

import org.apache.http.conn.scheme.HostNameResolver;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.MainActivityServiceProvider;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.model.HomeDataModel;
import com.weipu.common.facade.model.ModuleChildrenModel;
import com.weipu.common.okhttp.JsonUtils;
import com.weipu.common.util.JsonFormat;
import com.weipu.common.util.Logger;

/**
 * 首页接口
 *
 * @author huanfei
 * @version [版本号, 2016-4-8]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MainActivityServiceProviderImpl extends BaseServiceProviderImpl implements MainActivityServiceProvider {

    @Override
    public HomeDataModel getHomeData(String version) throws DxException {
        JSONObject dataObject = requestHttpGetDataByHeader(Constant.GETHOMEDATA_URL,
                ServerConstant.Parameter.PARAMETER_NAME_GETHOME_HEADER, version);
//        Log.i("---", dataObject.toString());
        //遗留问题 捕获空指针异常
        HomeDataModel homeData;
        try {
            homeData = JsonUtils.fromJson(dataObject.toString(), HomeDataModel.class);

        }catch (NullPointerException e){
            return null;
        }
//        Log.i("---", homeData.toString());
        return homeData;
    }

    @Override
    public ArrayList<ModuleChildrenModel> getActivityList(int pageIndex, int pageSize) throws DxException {

        JSONObject dataObject = requestHttpGetData(Constant.GETACTIVITYLIST_URL + "?pageIndex=" + pageIndex + "&pageSize=" + pageSize);
        JSONArray optJSONArray = dataObject.optJSONArray("Data");
        ArrayList<ModuleChildrenModel> list = JsonUtils.fromJson(optJSONArray.toString(), new TypeToken<ArrayList<ModuleChildrenModel>>() {
        }.getType());
        return list;
    }
}
